package com.example.myapplication.ui.components


import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewmodel.compose.viewModel
import com.example.myapplication.ViewModel.MainViewModel
import com.google.accompanist.pager.ExperimentalPagerApi
import com.google.accompanist.pager.VerticalPager
import com.google.accompanist.pager.rememberPagerState
import kotlinx.coroutines.launch
import java.util.*

@OptIn(ExperimentalPagerApi::class)
@Composable
fun NotificationContent(vm: MainViewModel = viewModel()) {
    // 虚拟页数
    // Int.MAX_VALUE int型的最大值
    // 直接设置virtualPage,会导致真实数据list越界(找不到这么大的值),应用会闪退
    val virtualPage = Int.MAX_VALUE

    // 实际页数
    val actualPage = vm.notifications.size

    // 初始图片下标
    // 取总数中间的值
    val initialIndex = virtualPage / 2

    // 轮播图初始位置
    val pagerState = rememberPagerState(initialPage = initialIndex)

    val coroutineScope = rememberCoroutineScope()

    // 自动轮播定时器(需要销毁,故使用DisposableEffect)
    DisposableEffect( Unit ) {
        var timer = Timer()

        timer.schedule(object : TimerTask() {
            override fun run() {
                coroutineScope.launch {
                    // scrollToPage 滚动到某一页
                    // scrollToPage挂起函数需要协程
                    pagerState.scrollToPage(pagerState.currentPage + 1)
                }
            }
        }, 2000, 3000)

        // 销毁定时器
        onDispose {
            timer.cancel()
        }
    }

    Row(
        // 第一个padding是在背景之前设置,第二个是在背景之后
        // 必须是现先有圆角才能有背景,要不然圆角不会出来
        modifier = Modifier
            .padding(8.dp)
            .clip(RoundedCornerShape(8.dp))
            .background(Color(0x22149EE7))
            .height(45.dp)
            .padding(horizontal = 8.dp),
        verticalAlignment = Alignment.CenterVertically,
        horizontalArrangement = Arrangement.SpaceBetween
    ) {
        Text(text = "最新活动", color = Color(0xFF149EE7), fontSize = 14.sp)
        
        Spacer(modifier = Modifier.width(8.dp))
        
        // VerticalPager 纵向滚动
        VerticalPager(count = virtualPage, state = pagerState, modifier = Modifier.weight(1f)) { index ->
            val currentIndex = (index - initialIndex).floorMod(actualPage)
            Text(
                text = vm.notifications[currentIndex],
                color = Color(0xFF333333),
                fontSize = 14.sp,
                maxLines = 1,
                overflow = TextOverflow.Ellipsis,
                modifier = Modifier.fillMaxWidth()
            )
        }

        Spacer(modifier = Modifier.width(8.dp))

        Text(
            text = "更多",
            color = Color(0xFF666666),
            fontSize = 14.sp,
            maxLines = 1
        )
    }
}
